package com.garlic.face.jlf.transaction;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.garlic.face.jlf.transaction.constances.JLFConstant;
import com.garlic.face.jlf.transaction.dataInterfaces.CancelBarcodeRequest;
import com.garlic.face.jlf.transaction.dataInterfaces.CancelTradeRequest;
import com.garlic.face.jlf.transaction.dataInterfaces.GoodRequest;
import com.garlic.face.jlf.transaction.dataInterfaces.LoginRequest;
import com.garlic.face.jlf.transaction.dataInterfaces.PayRequest;
import com.garlic.face.jlf.transaction.dataInterfaces.StateRequest;
import com.garlic.face.jlf.transaction.dataInterfaces.SubTotalRequest;
import com.garlic.face.jlf.transaction.dataInterfaces.SuperVisorRequest;
import com.garlic.face.jlf.transaction.dataInterfaces.TradeRequest;
import com.garlic.face.jlf.transaction.dataInterfaces.VipRequest;
import com.garlic.face.jlf.utils.LogUtils;
import com.google.gson.Gson;


public class CarrfourUtil {
    private static CarrfourUtil carrfourUtil;

    public static CarrfourUtil getInstance() {
        if (carrfourUtil == null) {
            synchronized (CarrfourUtil.class) {
                if (carrfourUtil == null) {
                    carrfourUtil = new CarrfourUtil();
                }
            }
        }
        return carrfourUtil;
    }

    private String sendMsg = "";

    public void setSendMsg(String sendMsg) {
        this.sendMsg = sendMsg;
    }

    public String getSendMsg() {
        return sendMsg;
    }

    public String getStateInfo() {
        StateRequest stateRequest = new StateRequest();
        stateRequest.setINTERFACETYPE(JLFConstant.TAG_STATE_REQUEST);
        stateRequest.setPosno(Means.getInstance().getPosno());
        Gson gson = new Gson();
        return gson.toJson(stateRequest);
    }

    public String getLoginInfo(String cashier, String cashierpwd) {
        LoginRequest loginRequest = new LoginRequest();
        loginRequest.setINTERFACETYPE(JLFConstant.TAG_LOGIN_REQUEST);
        loginRequest.setCashier(cashier);
        loginRequest.setCashierpwd(cashierpwd);
        loginRequest.setFlag("0");
        loginRequest.setOperators("");
        loginRequest.setPassword("");
        loginRequest.setPosno(Means.getInstance().getPosno());
        loginRequest.setShopno(Means.getInstance().getShopno());
        Gson gson = new Gson();
        return gson.toJson(loginRequest);
    }

    public String getLogoutInfo(String cashier, String cashierpwd, String supervisor, String supervisorPwd) {
        LoginRequest loginRequest = new LoginRequest();
        loginRequest.setINTERFACETYPE(JLFConstant.TAG_LOGIN_REQUEST);
        loginRequest.setCashier(cashier);
        loginRequest.setCashierpwd(cashierpwd);
        loginRequest.setFlag("1");
        loginRequest.setOperators(supervisor);
        loginRequest.setPassword(supervisorPwd);
        loginRequest.setPosno(Means.getInstance().getPosno());
        loginRequest.setShopno(Means.getInstance().getShopno());
        Gson gson = new Gson();
        return gson.toJson(loginRequest);
    }

    public String getVipInfo(String cardno, String type) {
        VipRequest vipRequest = new VipRequest();
        vipRequest.setINTERFACETYPE(JLFConstant.TAG_VIP_REQUEST);
        vipRequest.setCertifytype(type);
        vipRequest.setPosno(Means.getInstance().getPosno());
        vipRequest.setTransnmbr(String.valueOf(Means.getInstance().getRec_Tran().getTransnmbr()));
        vipRequest.setVipnmbr(cardno);
        Gson gson = new Gson();
        return gson.toJson(vipRequest);
    }

    public String getGoodInfo(String barcode, String qty) {
        GoodRequest goodRequest = new GoodRequest();
        goodRequest.setINTERFACETYPE(JLFConstant.TAG_GOOD_REQUEST);
        goodRequest.setActivation("");
        goodRequest.setBarcode(barcode);
        goodRequest.setFlow_id(String.valueOf(Means.getInstance().getItemCount()));
        goodRequest.setPosno(Means.getInstance().getPosno());
        goodRequest.setQty(qty);
        goodRequest.setTransnmbr(String.valueOf(Means.getInstance().getRec_Tran().getTransnmbr()));
        Gson gson = new Gson();
        return gson.toJson(goodRequest);
    }

    public String getTradList() {
        TradeRequest tradeRequest = new TradeRequest();
        tradeRequest.setINTERFACETYPE(JLFConstant.TAG_TRADE_REQUEST);
        tradeRequest.setPosno(Means.getInstance().getPosno());
        tradeRequest.setShopno(Means.getInstance().getShopno());
        tradeRequest.setTransnmbr(String.valueOf(Means.getInstance().getRec_Tran().getTransnmbr()));
        Gson gson = new Gson();
        return gson.toJson(tradeRequest);
    }

    public String cancelBarcode(String supervisor, String password, String barcode, String qty) {
        CancelBarcodeRequest cancelBarcodeRequest = new CancelBarcodeRequest();
        cancelBarcodeRequest.setINTERFACETYPE(JLFConstant.TAG_CANCELGOOD_REQUEST);
        cancelBarcodeRequest.setBarcode(barcode);
        cancelBarcodeRequest.setOperators(supervisor);
        cancelBarcodeRequest.setPassword(password);
        cancelBarcodeRequest.setPosno(Means.getInstance().getPosno());
        cancelBarcodeRequest.setQty(qty);
        cancelBarcodeRequest.setTransnmbr(String.valueOf(Means.getInstance().getRec_Tran().getTransnmbr()));
        Gson gson = new Gson();
        return gson.toJson(cancelBarcodeRequest);
    }

    public String cancelTrade(String supervisor, String password) {
        CancelTradeRequest cancelTradeRequest = new CancelTradeRequest();
        cancelTradeRequest.setINTERFACETYPE(JLFConstant.TAG_CANCELTRADE_REQUEST);
        cancelTradeRequest.setOperators(supervisor);
        cancelTradeRequest.setPassword(password);
        cancelTradeRequest.setPosno(Means.getInstance().getPosno());
        cancelTradeRequest.setTransnmbr(String.valueOf(Means.getInstance().getRec_Tran().getTransnmbr()));
        Gson gson = new Gson();
        return gson.toJson(cancelTradeRequest);
    }

    public String supervisor(String supervisor, String password, String type) {
        SuperVisorRequest superVisorRequest = new SuperVisorRequest();
        superVisorRequest.setINTERFACETYPE(JLFConstant.TAG_SUPERVISOR_REQUEST);
        superVisorRequest.setActiontype(type);
        superVisorRequest.setOperators(supervisor);
        superVisorRequest.setPassword(password);
        superVisorRequest.setPosno(Means.getInstance().getPosno());
        superVisorRequest.setTransnmbr(String.valueOf(Means.getInstance().getRec_Tran().getTransnmbr()));
        Gson gson = new Gson();
        return gson.toJson(superVisorRequest);
    }

    public String getSubtotal(String count, String qty) {
        SubTotalRequest subTotalRequest = new SubTotalRequest();
        subTotalRequest.setINTERFACETYPE(JLFConstant.TAG_SUBTOTAL_REQUEST);
        subTotalRequest.setCount(count);
        subTotalRequest.setPosno(Means.getInstance().getPosno());
        subTotalRequest.setQty(qty);
        subTotalRequest.setTransnmbr(String.valueOf(Means.getInstance().getRec_Tran().getTransnmbr()));
        Gson gson = new Gson();
        return gson.toJson(subTotalRequest);
    }

    public String getPayInfo(String payno, String payid, String total, String paymm, String verifycode, String flag, String yftotal, String ftoken) {
        PayRequest payRequest = new PayRequest();
        payRequest.setINTERFACETYPE(JLFConstant.TAG_PAY_REQUEST);
        payRequest.setAmount(total);
        payRequest.setBatchno("");
        payRequest.setFlag("0");
        payRequest.setKeyindex(payno);
        payRequest.setMchtid("");
        payRequest.setPayflag(flag);
        payRequest.setPayid(payid);
        payRequest.setPaymm(paymm);
        payRequest.setPosno(Means.getInstance().getPosno());
        payRequest.setReference("");
        if (payno.equals("3")) {
            payRequest.setScanned(payid);
        } else {
            payRequest.setScanned("");
        }

        payRequest.setTermid("");
        payRequest.setTrace("");
        payRequest.setTransnmbr(String.valueOf(Means.getInstance().getRec_Tran().getTransnmbr()));
        payRequest.setVerifycode(verifycode);
        payRequest.setYftotal(yftotal);
        payRequest.setFacescanToken(ftoken);
        LogUtils.d("xxxxxx payRequest:" + payRequest, true);
//        Gson gson = new Gson();
//        return gson.toJson(payRequest);
        return JSON.toJSONString(payRequest);
    }
}
